<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="btnaddBrand">添加</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>
    <!-- table -->
    <el-table stripe style="width: 100%" :data="browserTable" :key="key" border fit highlight-current-row ref="tableDataRef">
      <el-table-column v-if="colData[0].istrue" min-width="110" align="center" prop="username" label="归属"></el-table-column>
      <el-table-column v-if="colData[1].istrue" min-width="160" align="center" prop="version_number" label="固件版本号"> </el-table-column>
      <el-table-column v-if="colData[2].istrue" min-width="180" align="center" prop="package_name" label="内置APP"> </el-table-column>
      <el-table-column v-if="colData[3].istrue" min-width="140" align="center" prop="add_time" label="日期">
        <template slot-scope="scope">{{scope.row.add_time| newDate}}</template>
      </el-table-column>
      <el-table-column min-width="150" align="center" prop="log" label="操作">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" @click="editFirmwareAppId(scope.row)">编辑</el-button>
            <el-button size="mini" @click="getdeleteBrowser(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination background :current-page.sync="listQuery.startSize" @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]" layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>
    <el-dialog :title="editSubjects ? '固件内置APP' : '编辑'" :visible.sync="dialogFormVisible" @close="addFormclose" :close-on-click-modal="false" :close-on-press-escape="false" width="35%">
      <el-form :model="addForm" ref="ruleForm" :rules="rules">
        <el-form-item label="输入固件:" label-width="100px" prop="version_number">
          <el-input v-model="addForm.version_number" autocomplete="off" class="add_select" placeholder="请输入固件"></el-input>
        </el-form-item>
        <el-form-item label="输入包名:" label-width="100px" prop="package_name">
          <el-select class="selectStyle" v-model="addForm.package_name" :popper-append-to-body="false" remote multiple allow-create default-first-option popper-class="selectPopper" filterable size="medium"
            placeholder="请输入包名,用回车添加" style="width:100%"></el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size="mini" round>取 消</el-button>
        <el-button v-if="editSubjects" type="primary" @click="getaddFirmwareApp('ruleForm')" size="mini" round>确定</el-button>
        <el-button v-else type="primary" @click="geteditFirmwareApp('ruleForm')" size="mini" round>确定</el-button>
      </div>
    </el-dialog>
    <!-- 删除弹窗 -->
    <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents" :deleteText="deleteDialogs.deleteText" />
  </div>
</template>

<script>
import dynamicTable from "@/components/DynamicTable";
import objectable from "@/utils/objectable.js";
import { queryFirmwareApp, addFirmwareApp, editFirmwareApp, deleteFirmwareApp } from "@/api/allocation.js";
import deleteDialog from "@/components/deleteDialog";
export default {
  components: { dynamicTable, deleteDialog },
  data() {
    return {
      checkboxVal: ["归属", "固件版本号", "内置APP", "日期"],
      key: 1,
      colData: [], //动态控制table
      total: 0,
      //归属table
      browserTable: [],
      subjectId: 0,
      listQuery: { startSize: 1, pageSize: 10 },
      //添加按钮控制
      dialogFormVisible: false,
      //编辑按钮控制
      editSubjects: true,
      //浏览器专题id
      subjectId: 0,
      addForm: { version_number: undefined, package_name: undefined, userId: localStorage.getItem("ID") },
      rules: {
        version_number: [{ required: true, message: "请输入固件", trigger: "blur" }],
        package_name: [{ required: true, message: "请输入包名,用回车添加", trigger: "blur" }]
      },
      //删除弹窗
      deleteDialogs: { show: false, deleteText: '', deleteId: 0 },
    }
  },
  created() {
    this.colData = objectable(this.checkboxVal, this.checkboxVal);
    this.getqueryFirmwareApp()
  },
  methods: {
    //删除
    getdeleteBrowser(id) {
      this.deleteDialogs.show = true
      this.deleteDialogs.deleteId = id
    },
    //删除按钮事件
    deleteEvents() {
      let params = { id: this.deleteDialogs.deleteId }
      deleteFirmwareApp(params).then(res => {
        this.$message.success(res.msg)
        this.getqueryFirmwareApp()
      })
    },
    btnaddBrand() {
      this.editSubjects = true
      this.dialogFormVisible = true
    },
    getaddFirmwareApp(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let addForm = JSON.parse(JSON.stringify(this.addForm))
          addForm.package_name = addForm.package_name.toString()
          addFirmwareApp(addForm).then(res => {
            this.$message.success(res.msg)
            this.getqueryFirmwareApp()
            this.dialogFormVisible = false
          }).catch(err => { })
        } else {
          return false;
        }
      });
    },
    //编辑
    editFirmwareAppId(row) {
      this.subjectId = row.id
      this.editSubjects = false
      this.addForm.version_number = row.version_number
      this.addForm.package_name = row.package_name.split(',')
      this.dialogFormVisible = true
    },
    geteditFirmwareApp(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let addForm = JSON.parse(JSON.stringify(this.addForm))
          addForm.id = this.subjectId
          addForm.package_name = addForm.package_name.toString()
          editFirmwareApp(addForm).then(res => {
            this.dialogFormVisible = false
            this.$message.success(res.msg)
            this.getqueryFirmwareApp()
          }).catch(err => { })
        } else {
          return false
        }
      })
    },
    getqueryFirmwareApp() {
      queryFirmwareApp(this.listQuery).then(res => {
        this.browserTable = res.data
        this.total = res.total
      }).catch(err => {
        this.browserTable = []
        this.total = 0
      })
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getqueryFirmwareApp();
    },
    handleCurrentChange(val) {
      this.listQuery.startSize = val
      this.getqueryFirmwareApp();
    },
    Refresh() {
      this.listQuery = { startSize: 1, pageSize: 10 }
      this.getqueryFirmwareApp()
    },
    addFormclose() {
      this.$nextTick(() => {
        this.$refs.ruleForm.resetFields();
        this.addForm = { version_number: undefined, package_name: undefined, userId: localStorage.getItem("ID") }
      })
    },
    //下拉动态table子组件
    alterCheckbok(key, conte) {
      this.key = key;
      this.colData.filter(i => {
        if (conte.indexOf(i.title) != -1) {
          i.istrue = false;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        } else {
          i.istrue = true;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        }
      });
    }
  }
}
</script>

<style scoped>
/deep/ .selectPopper {
  display: none;
}
</style>